// pages/editInformation/editInformation.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

  data: {
    form_ages_class: "",
    form_height_class: "",
    form_bodyWeight_class: "",
    form_position_class: "",
    photoImg: '',//头像
    session_key: '',//token
    user_info:'',//获取的用户信息
    userName:'',//名字
    phoneNum:'',//电话
    nowdate:'',//当前日期
    _date:'',//日期
    heightNum:'',//身高
    bodyWeightNum: '',//体重
    positionId: [],//场上位置的id
    positionName: [],//场上位置的name
    positionIndex: 0,//场上位置显示的索引
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var _DATE = utils.formatDate(new Date());
    that.setData({
      session_key: wx.getStorageSync("token"),
      nowdate: _DATE
    })
    //获取个人信息
    wx.request({
      url: app.globalData.httpsPath + '/user/info',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: that.data.session_key
      },
      //请求后台数据成功  
      success: function (res) {
        var _info = res.data.data;
        console.info(_info);
        that.setData({
          user_info: _info,
          photoImg: _info.avatar,
          userName: _info.name,//名字
          phoneNum: _info.phone,//电话
          _date: _info.birthday,//日期
          heightNum: _info.height,//身高
          bodyWeightNum: _info.weight,//体重
          positionIndex: _info.position_id-1,//场上位置显示的索引
        })
      }
    })
    //获取场上位置
    wx.request({
      url: app.globalData.httpsPath + '/position', // 接口地址
      method: "POST",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        var posidArry = [];
        var posArry = [];
        var positionList = res.data.data;
        for (var i = 0; i < positionList.length; i++) {
          posidArry.push(positionList[i].id);
          posArry.push(positionList[i].name);
        }
        that.setData({
          positionId: posidArry,
          positionName: posArry
        })
      }
    })
  },
  // 上传logo
  uploadPhoto: function () {
    var that = this;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        var avatar = res.tempFilePaths;
        wx.uploadFile({
          url: app.globalData.httpsPath + '/upload',
          method: "POST",
          filePath: avatar[0],
          name: 'pic',
          header: {
            "content-type": "multipart/form-data"
          },
          formData: {
            //和服务器约定的token, 一般也可以放在header中
            'token': that.data.session_key
          },
          success: function (res) {
            var _imgdata = JSON.parse(res.data);
            if (res.statusCode != 200) {
              wx.showModal({
                title: '提示',
                content: '上传失败',
                showCancel: false
              })
              return;
            }
            var _imgpath = _imgdata.data
            that.setData({  //上传成功修改显示头像
              photoImg: app.globalData.imagePath + _imgpath
            })
          },
          fail: function (e) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
          },
          complete: function () {
            wx.hideToast();  //隐藏Toast
          }
        });
      }
    })
  },
  //获取用户输入的用户名
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  //获取用户输入的手机号
  phoneInput: function (e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },
  //日期
  bindDateChange: function (event) {
    this.setData({
      _date: event.detail.value
    })
  },
  //获取身高
  heightInput: function (e) {
    this.setData({
      heightNum: e.detail.value
    })
  },
  //获取体重
  bodyWeightInput: function (e) {
    this.setData({
      bodyWeightNum: e.detail.value
    })
  },
  //获取用户场上位置
  bindPickerChange(e) {
    this.setData({
      positionIndex: e.detail.value
    })
  },
  //保存
  saveBtn:function(){
    var that = this;
    var _postionId = that.data.positionId;
    var _selected_id = _postionId[that.data.positionIndex];
    if (that.data.userName == '') {
      wx.showToast({
        icon: "none",
        title: '请输入您的姓名'
      })
      return false;
    }
    if (that.data.phoneNum == '') {
      wx.showToast({
        icon: "none",
        title: '请输入您的手机号'
      })
      return false;
    }
    if (that.data._date == '') {
      wx.showToast({
        icon: "none",
        title: '请输入您的出生日期'
      })
      return false;
    }
    if (that.data.heightNum == '') {
      wx.showToast({
        icon: "none",
        title: '请输入您的身高'
      })
      return false;
    }
    if (that.data.bodyWeightNum == '') {
      wx.showToast({
        icon: "none",
        title: '请输入您的体重'
      })
      return false;
    } 
    wx.request({
      url: app.globalData.httpsPath + '/user/update-info', // 接口地址
      method: "POST",
      data: {
        token: that.data.session_key, 
        name: that.data.userName,
        phone: that.data.phoneNum,
        avatar: that.data.photoImg,
        birthday: that.data._date,
        height: that.data.heightNum,
        weight: that.data.bodyWeightNum,
        position_id: _selected_id,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.info(res.data)
        wx.showToast({
          title: '修改成功',
          icon: 'succes',
          duration: 3000,
          mask: true
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '../personalCenter/personalCenter'
          })
        }, 3000)

      }
    })
  },
   //取消
  backBtn: function () {
    wx.reLaunch({
      url: '../personalCenter/personalCenter'
    })
  }
})